<template>
  <div>
    <div style="display: flex;padding:10px 0;margin: 0 0 5px; ">

      <kv label="语言">
        <el-tag>{{ $props.language }}</el-tag>
      </kv>
      <kv label="产品线">
        <product-line :language="$props.language"
                      v-model="$data.query.productLineCode" chk="*"
                      @change="productLine_change"></product-line>
        <!--				<selector type="radio" v-model="query.productLineCode" :data="products" fields="code,name"-->
        <!--				          :enumCount="2" @change="productLine_change"-->
        <!--				          chk="*"></selector>-->
      </kv>

      <kv label="git仓库">
        <selector v-model="$data.query.gitId" :data="lang_gits"
                  fields="id,name"
                  @change="git_change"
                  :enum-count="1"
                  chk="*"></selector>
      </kv>

      <div class="kv" v-if="$data.query.gitId">
        <el-button @click="reload()">重新加载</el-button>
        <el-button @click="jv.$router.push('/dev/git-data/edit/' + $data.query.gitId)">修改</el-button>
      </div>
    </div>
    <jenkins-com v-if="$data.git.id" :key="$data.git.id + $data.version"
                 :gitInfo="$data.git"></jenkins-com>

  </div>
</template>

<script type="text/ecmascript-6" setup>

import jenkinsCom from "./jenkins_com.vue";
import productLine from "@/component/product-line.vue";
import appEnv from "@/component/app-env.vue";


defineOptions({
  name: "deploy",
  inheritAttrs: true,
});

//定义 vue2的this,当前组件
const $self = getCurrentInstance();

//定义 $attrs
const $attrs = useAttrs();


//定义data
const $data = reactive({
  version: 0,
  targetEnv: "",
  product_range: "",
  isAdmin: false,
  type: "Java",
  data: {},
  gitDatas: [],
  git: {},
  query: {productLineCode: "", gitId: ""},
  products: []
});


const $props = defineProps({
  language: {type: String}
});

const list = ref();

//定义事件
const $emit = defineEmits(["update:modelValue", "loaded"]);


const reload = function () {
  $data.version++;
};
const proc_data = function (data) {
  data.removeItem(it => !it.url);
  data.unshift({id: "", code: "", name: "[当前K8s服务器]", url: ""})
};
const loadDataFromUrl = function () {
  var search = jv.$loadData("location") || "";
  if (location.search.length <= 1 && search.length > 1) {
    jv.saveToHistoryUrlQuery(window, search, true);
  } else {
    $data.query.productLineCode = "";
    $data.query.gitId = "";
    $data.git = {};
  }

  var topJson = jv.query2Json(top.location.href);
  var productLineCode = topJson["productLineCode"];

  if (productLineCode) {
    $data.query.productLineCode = productLineCode
  }

  var gitId = topJson["gitId"];
  if (gitId) {
    $data.query.gitId = gitId
  }
};
const open_new = function () {
  window.open(window.location.href, "_blank")
};
const git_change = function (v, init) {
  $data.git = $data.gitDatas.find(it => it.id == v) || {};
  if (!$data.git.folders) {
    return;
  }

  // $data.git.folders.forEach(it=>{
  //   it.fullName = it.name + "|" + it.cnname
  // });

  $data.git.folders
      .groupBy(it => {
        return it.path
      })
      .forEach(it => {
        if (it.data.length > 1) {
          it.data.forEach(it => {
            it.path += " (" + it.cnname + ")"
          })
        }
      });

  var def = {};
  if (jv.asBoolean(top.location.json._com_)) {
    def._com_ = true;
  }

  jv.saveToHistoryUrlQuery(top, Object.assign(def, $data.query), init === true ? false : true)
  jv.$saveData("location", location.search)
};
const productLine_change = async function (v, m, p) {
  await loadGitInfo();
};
const loadGitInfo = async function (init) {
  if (!Object.keys($data.data).length) {
    var d = await jv.$http.post("/dev/git-data/list", {take: 99, language: $props.language});
    $data.data = d.data.data;
  }
  var productLineCode = $data.query.productLineCode;
  $data.git = {};
  $data.gitDatas = $data.data.filter(it => it.productLine.code == productLineCode) || [];


  git_change($data.query.gitId, init);
};

watch(() => $props.language, async (v, o) => {
  loadDataFromUrl();
  $data.isAdmin = jv.me.user.isAdmin;
  jv.targetEnv = $data.targetEnv;
  await loadGitInfo(!o);
  $data.version++;
}, {immediate: true})

const lang_gits = $computed(() => {
  var data = $data.gitDatas, lang = $props.language;
  if (!lang) {
    return data || [];
  }
  return data.filter(it => it.language == lang) || [];
});


</script>

<style scoped lang="scss">

.no-width :deep(.v) > :first-child {
  max-width: 100%;
}
</style>
